<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="Content-Language" content="zh-CN"><link href="stylesheet.css" media="all" rel="stylesheet" type="text/css">
<title>组合查询</title>
<script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d286c55b63a3c54a1e43d10d4c203e75"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script>
</head><body class="SECT1">
<div>
<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><th colspan="5" align="center" valign="bottom">PostgreSQL 8.2.3 中文文档</th></tr>
<tr><td width="10%" align="left" valign="top"><a href="queries-select-lists.html" accesskey="P">后退</a></td><td width="10%" align="left" valign="top"><a href="queries.html">快退</a></td><td width="60%" align="center" valign="bottom">章7. 查询</td><td width="10%" align="right" valign="top"><a href="queries.html">快进</a></td><td width="10%" align="right" valign="top"><a href="queries-order.html" accesskey="N">前进</a></td></tr>
</table>
<hr align="LEFT" width="100%"></div>
<div class="SECT1"><h1 class="SECT1"><a name="QUERIES-UNION">7.4. 组合查询</a></h1><a name="AEN3485"></a><a name="AEN3487"></a><a name="AEN3489"></a><a name="AEN3491"></a><a name="AEN3493"></a><a name="AEN3495"></a><a name="AEN3497"></a>
<p>可以对两个查询的结果进行集合操作(并、交、差)。语法是：</p>
<pre class="SYNOPSIS"><tt class="REPLACEABLE"><i>query1</i></tt> UNION [<span class="OPTIONAL">ALL</span>] <tt class="REPLACEABLE"><i>query2</i></tt>
<tt class="REPLACEABLE"><i>query1</i></tt> INTERSECT [<span class="OPTIONAL">ALL</span>] <tt class="REPLACEABLE"><i>query2</i></tt>
<tt class="REPLACEABLE"><i>query1</i></tt> EXCEPT [<span class="OPTIONAL">ALL</span>] <tt class="REPLACEABLE"><i>query2</i></tt></pre>
<p><tt class="REPLACEABLE"><i>query1</i></tt> 和 <tt class="REPLACEABLE"><i>query2</i></tt> 可以是讨论过的所有查询。集合操作也可以嵌套和级连，比如：</p>
<pre class="SYNOPSIS"><tt class="REPLACEABLE"><i>query1</i></tt> UNION <tt class="REPLACEABLE"><i>query2</i></tt> UNION <tt class="REPLACEABLE"><i>query3</i></tt></pre>
<p>它实际上等价于</p>
<pre class="SYNOPSIS">(<tt class="REPLACEABLE"><i>query1</i></tt> UNION <tt class="REPLACEABLE"><i>query2</i></tt>) UNION <tt class="REPLACEABLE"><i>query3</i></tt></pre>
<p><tt class="LITERAL">UNION</tt> 把 <tt class="REPLACEABLE"><i>query2</i></tt> 的结果附加到 <tt class="REPLACEABLE"><i>query1</i></tt> 的结果上(不过我们不能保证这就是这些行实际的返回顺序)，并且像 <tt class="LITERAL">DISTINCT</tt> 那样删除结果中所有重复的行(除非声明了 <tt class="LITERAL">UNION ALL</tt>)。</p>
<p><tt class="LITERAL">INTERSECT</tt> 返回那些同时存在于 <tt class="REPLACEABLE"><i>query1</i></tt> 和 <tt class="REPLACEABLE"><i>query2</i></tt> 结果中的行，除非声明了 <tt class="LITERAL">INTERSECT ALL</tt> ，否则所有重复行都被删除。</p>
<p><tt class="LITERAL">EXCEPT</tt> 返回所有在 <tt class="REPLACEABLE"><i>query1</i></tt> 结果中但是不在 <tt class="REPLACEABLE"><i>query2</i></tt> 结果中的行(有时侯这叫做两个查询的<i class="FIRSTTERM">差</i>)。除非声明了 <tt class="LITERAL">EXCEPT ALL</tt> ，否则所有重复行都被删除。</p>
<p>为了能够计算两个查询的并、交、差，这两个查询必须是"并集兼容的"，也就是它们都返回同样数量的列，并且对应的列有兼容的数据类型，就像<a href="typeconv-union-case.html">节10.5</a>里描述的那样。</p>
</div>
<div>
<hr align="LEFT" width="100%">
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td width="33%" align="left" valign="top"><a href="queries-select-lists.html" accesskey="P">后退</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">首页</a></td><td width="33%" align="right" valign="top"><a href="queries-order.html" accesskey="N">前进</a></td></tr>
<tr><td width="33%" align="left" valign="top">选择列表</td><td width="34%" align="center" valign="top"><a href="queries.html" accesskey="U">上一级</a></td><td width="33%" align="right" valign="top">行排序</td></tr>
</table>
</div>
</body></html>